/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.service;

import br.com.foxline.clinicplus.entity.*;
import br.com.foxline.clinicplus.enums.Reports;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
public class RecordsService extends GenericService<Records> {

    @EJB
    ClinicService clinicService;
    
    public RecordsService() {
        super(Records.class);
    }

//    @Override
//    public void update(Records entity) {
//        if (entity.getAnamneses() != null && !entity.getAnamneses().isEmpty()) {
//            for (Anamnese anamnese : entity.getAnamneses()) {
//                System.err.println("ANAMENESE: "+anamnese);
//                getEntityManager().merge(anamnese);
//                getEntityManager().flush();
//            }
//        }
//        if (entity.getPhysicalExamination() != null) {
//            getEntityManager().merge(entity.getPhysicalExamination());
//            getEntityManager().flush();
//        }
//        if (entity.getDiagnostic() != null) {
//            getEntityManager().merge(entity.getDiagnostic());
//            getEntityManager().flush();
//
//        }
//        if (entity.getEvolutions() != null && !entity.getEvolutions().isEmpty()) {
//            for (Evolution e : entity.getEvolutions()) {
//                getEntityManager().merge(e);
//                getEntityManager().flush();
//            }
//        }
//        
//        getEntityManager().merge(entity);
//        getEntityManager().flush();
//    }
//
//    @Override
//    public void save(Records entity) {
//
//        if (entity.getAnamneses() != null && entity.getAnamneses().isEmpty()) {
//            for (Anamnese anamnese : entity.getAnamneses()) {
//                getEntityManager().persist(anamnese);
//            }
//        }
//        if (entity.getPhysicalExamination() != null) {
//            getEntityManager().persist(entity.getPhysicalExamination());
//        }
//        if (entity.getDiagnostic() != null) {
//            getEntityManager().persist(entity.getDiagnostic());
//        }
//        if (entity.getEvolutions() != null && entity.getEvolutions().isEmpty()) {
//            for (Evolution e : entity.getEvolutions()) {
//                getEntityManager().persist(e);
//            }
//        }
//
//        super.save(entity);
//    }
    public Diary getDiaryRecords(Long records) {
        String sql = "Select distinct(d) from Diary d JOIN d.records r where r.id = :records"
                + " order by d.createAt desc";
        Query createQuery = getEntityManager().createQuery(sql).setMaxResults(1);
        createQuery.setParameter("records", records);
        try {
            return (Diary) createQuery.getSingleResult();
        } catch (Exception e) {
            System.err.println("*******"+records);
//            e.printStackTrace();
            
            return null;
        }
    }

    public List<Records> getHistoryRecords(Customer customer) {
        String sql = "Select r from Records r JOIN r.customer c where c.id = :customer"
                + " order by r.id desc";
        Query createQuery = getEntityManager().createQuery(sql);
        createQuery.setParameter("customer", customer.getId());
        return createQuery.getResultList();
    }

    public String getLastPrescriptionRecords(Customer customer) {
        String sql = "Select r.prescription from Records r JOIN r.customer c where c.id = :customer"
                + " order by r.id desc";
        Query createQuery = getEntityManager().createQuery(sql);
        createQuery.setParameter("customer", customer.getId());
        String resul;
        try {
            resul = (String) createQuery.getResultList().get(0);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

        return resul;
    }

    //RELATÓRIO
    public void reportsPrescription(Records records) {
        HashMap<String, Object> parametros;
        parametros = new HashMap<String, Object>();
        List<Records> record = new ArrayList<Records>();
        record.add(records);
        parametros = clinicService.getCabecalho(parametros);
        ReportService.genReport(parametros, record, Reports.PRESCRIPTION);
    }
    //RELATÓRIO
    public void reportsPrescriptionEspecial(Records records) {
        HashMap<String, Object> parametros;
        parametros = new HashMap<String, Object>();
        List<Records> record = new ArrayList<Records>();
        record.add(records);
        parametros = clinicService.getCabecalho(parametros);
        ReportService.genReport(parametros, record, Reports.RECEITUARIO_ESPECIAL);
    }

    public void reportsRecords(Customer customer) {
        HashMap<String, Object> parametros;
        parametros = new HashMap<String, Object>();
        parametros.put("customer.name", customer.getName());
        parametros.put("customer.cpf", customer.getCpf());
        parametros.put("customer.dateBirth", customer.getDateBirth());
        InputStream imgInputStream = null;

        if (customer.getAnnex() != null) {
            imgInputStream = new ByteArrayInputStream(customer.getAnnex().getFileByte());
            parametros.put("customer.annex.fileByte", imgInputStream);
        } else {
            //File file = new File("");
            //imgInputStream = new ByteArrayInputStream(customer.getAnnex().getFileByte());
        }

        List<Records> record = getHistoryRecords(customer);
        
        parametros = clinicService.getCabecalho(parametros);
        ReportService.genReport(parametros, record, Reports.RECORDS);
    }
}
